package com.cs157a.view;

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import com.cs157a.dao.RdiPart;

public class PartListPanel extends JPanel {
	private static final long serialVersionUID = 20012100301L;

	private JTable table;
	private PartTableModel tableModel;

	public PartListPanel(final List<RdiPart> parts) {
		tableModel = new PartTableModel(parts);
		table = new JTable(tableModel);
		table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		JScrollPane scrollPane = new JScrollPane(table);

		setLayout(new BorderLayout());
		add(scrollPane, BorderLayout.CENTER);
	}

	public List<RdiPart> getSelectedPart() {
		List<RdiPart> parts = new ArrayList<RdiPart>();
		int[] selectedRows = table.getSelectedRows();
		for (int i = 0; i < selectedRows.length; i++) {
			int modelIndex = table.convertRowIndexToModel(selectedRows[i]);
			RdiPart p = tableModel.getVAlueAt(modelIndex);
			parts.add(p);
		}

		return parts;
	}

	public void reset() {
		// TODO Auto-generated method stub
		table.setModel(new PartTableModel(new ArrayList<RdiPart>()));
	}

}
